<?php

class EpalShop_Controller extends Page_Controller {
    private static $allowed_actions = array(
        'view'
    );

    protected $URLSegment = 'shop';
    public static $URLCategory = 'shop/view';
    public static $URLProduct = 'shop/product';

    function getURLProduct(){
        return self::$URLProduct;
    }
    function getURLCategory(){
        return self::$URLCategory;
    }

    public function Link($action = null, $id = null) {
        $params = $this->request->params();

        return Controller::join_links($this->URLSegment, $params['Action'], $params['ID'], $params['OtherID']);
    }

    function getClassName(){ return 'EpalShop_Controller'; }

    function init(){
        parent::init();
    }

    function view(){
        $params = $this->request->params();

        if($params['ID'] == null ){//show all category
            return $this->customise(array('Categories' => ECategory::get()))->renderWith(array('ECategories', 'Page'));
        }else{

            $category_segment = $params['ID'];
            $category = ECategory::get()->filter(array('UrlSegment' => $category_segment))->first();

            if($params['OtherID'] == null){//show category detail

                $this->MetaTitle = $category->Title;

                $paginatedList = new PaginatedList($category->EProducts(), $this->request);
                $paginatedList->setPageLength(12);

                return $this->customise(array('Category' => $category, 'ProductPaginated' => $paginatedList))->renderWith(array('ECategory', 'Page'));

            }else{//show product detail
                $product_segment = $params['OtherID'];
                $product = EProduct::get()->filter(array('UrlSegment' => $product_segment))->first();
                $this->MetaTitle = $product->Title;

                return $this->customise(array('Category' => $category, 'Product' => $product))->renderWith(array('EProduct', 'Page'));
            }
        }
    }

    function EShopBreadCrumbs(){
        $params = $this->request->params();

        $breadCrumbArr = array(
            'ShopLink' => null,
            'CategoriesLink' => null,
            'CategoryTitle' => null,
            'CategoryLink' => null,
            'ProductTitle' => null,
            'ProductLink' => null
        );

        if($params['Action'] != null){
            $breadCrumbArr['ShopLink'] = $this->URLSegment;
            $breadCrumbArr['CategoriesLink'] = $this->URLCategory;
        }

        if($params['ID'] == null ){//show all category

        }else{

            $category_segment = $params['ID'];
            $category = ECategory::get()->filter(array('UrlSegment' => $category_segment))->first();

            if($params['OtherID'] == null){//show category detail
                $breadCrumbArr['CategoryTitle'] = $category->Title;
            }else{//show product detail
                $product_segment = $params['OtherID'];
                $product = EProduct::get()->filter(array('UrlSegment' => $product_segment))->first();
                $breadCrumbArr['CategoryTitle'] = $category->Title;
                $breadCrumbArr['CategoryLink'] = $category->CategoryLink();
                $breadCrumbArr['ProductTitle'] = $product->Title;
            }
        }

        $viewer = new SSViewer(array('EShopBreadCrumbs'));
        return $viewer->process(new ArrayData($breadCrumbArr));
    }

    function getListCategory(){
        return ECategory::get();
    }

    function getListNewProduct(){
        return EProduct::get()->sort('UpdateDate DESC');
    }

}